package com.leyou.sh.item.web;

import com.leyou.sh.common.dto.PageDTO;
import com.leyou.sh.item.dto.SpecGroupDTO;
import com.leyou.sh.item.dto.SpecParamDTO;
import com.leyou.sh.item.service.SpecGroupService;
import com.leyou.sh.item.service.SpecParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author balisong_fcs
 * @date 2020/12/28 20:24
 * @desc
 */
@RestController
@RequestMapping("/spec")
public class SpecGroupController {
    @Autowired
    private SpecGroupService specGroupService;
    @Autowired
    private SpecParamService specParamService;

    /**
     * 新增规格组
     * @param specGroupDTO
     * @return
     */
    @PostMapping("/group")
    public ResponseEntity<Void> saveSpecGroup(
            SpecGroupDTO specGroupDTO
    ){
        this.specGroupService.saveSpecGroup(specGroupDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 修改规格组
     * @param specGroupDTO
     * @return
     */
    @PutMapping
    public ResponseEntity<Void> updateSpecGroup (
            SpecGroupDTO specGroupDTO
    ){
        this.specGroupService.updateSpecGroup(specGroupDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 根据分类id查询规格组集合
     * 每个商品分类,对应多个规格组,是1对多
     * @param id
     * @return
     */
    @GetMapping("groups/of/category")
    public ResponseEntity<List<SpecGroupDTO>> querySpecGroupByCategory(
            @RequestParam("id") Long id
    ){
        return ResponseEntity.ok(this.specGroupService.querySpecGroupByCategory(id));
    }

    /**
     * 根据分类id查询规格组及其组内参数
     * 这里的方法没有设置对应的value值
     * @param id
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<List<SpecGroupDTO>> querySpecGroupAndSpecParamByCategory (
            @RequestParam("id") Long id
    ){
        return ResponseEntity.ok(this.specGroupService.querySpecGroupAndSpecParamByCategory(id));
    }

    /**
     * 根据条件查询规格参数集合
     * @param categoryId
     * @param groupId
     * @return
     */
    @GetMapping("/params")
    public ResponseEntity<List<SpecParamDTO>> querySpecParamByCidAndGid(
            @RequestParam(value = "categoryId",required = false) Long categoryId,
            @RequestParam(value = "groupId",required = false) Long groupId,
            @RequestParam(value = "searching",required = false) Boolean searching

    ){
        return ResponseEntity.ok(this.specParamService.querySpecParamByCidAndGid(categoryId,groupId,searching));
    }

    /**
     * 新增规格参数
     * @param specParamDTO
     * @return
     */
    @PostMapping("/param")
    public ResponseEntity<Void> addParam(
            @RequestBody SpecParamDTO specParamDTO
    ) {
        this.specParamService.addParam(specParamDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    @PutMapping("/param")
    public ResponseEntity<Void> updateParam(
            @RequestBody SpecParamDTO specParamDTO
    ){
        this.specParamService.updateParm(specParamDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

}
